package com.sgb.kjwl.view.ui.adapter.lease;

import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.LayoutLeaseSetBinding;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.lease.PriceSetEntity;
import com.sgb.kjwl.utils.EditInputFilter;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 价格动态设置适配器
 */
public class PriceSetAdapter extends BaseRecycleAdapter2<PriceSetEntity> {

    private Context context;
    private List<PickSelectEntity<List<PickSelectEntity>>> timeUnitList;
    private boolean isDriver = false;//是否显示自带驾驶员价格
    private int max = 4;
    private Map<String, String> selectedMap = new HashMap<>();
    private String categoryType = "0";//0，机械，1 设备

    public void setUnitList(List<PickSelectEntity<List<PickSelectEntity>>> unitList, boolean isDriver, String categoryType) {
        this.timeUnitList = unitList;
        this.isDriver = isDriver;
        this.categoryType = categoryType;
        this.max = unitList.size() > 4 ? 4 : unitList.size();
    }

    public PriceSetAdapter(Context context, List<PriceSetEntity> datas) {
        super(context, R.layout.layout_lease_set, datas);
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, PriceSetEntity priceSetEntity, int position) {
        holder.setBinding(BR.viewModel, priceSetEntity);
        LayoutLeaseSetBinding binding = (LayoutLeaseSetBinding) holder.getmBinding();

        if ("0".equals(categoryType)) {
            binding.itemTvPriceDriver.setLabelName("自带驾驶员价格(不含税)");
            binding.itemTvPriceDriverNot.setLabelName("不自带驾驶员价格(不含税)");
        }else if ("1".equals(categoryType)) {
            binding.itemTvPriceDriver.setLabelName("租赁价格");
            binding.itemTvPriceDriverNot.setLabelName("租赁价格");
            binding.itemTvPriceDriver.setLabelHint("请输入价格(不含税,不含操作员)");
            binding.itemTvPriceDriverNot.setLabelHint("请输入价格(不含税,不含操作员)");
        }

        if (position == 0) {
            if (datas.size() > 1) {
                binding.tvAdd.setVisibility(View.GONE);
                binding.tvDel.setVisibility(View.VISIBLE);
            } else {
                binding.tvAdd.setVisibility(View.VISIBLE);
                binding.tvDel.setVisibility(View.GONE);
            }
        } else if (position == datas.size() - 1 && datas.size() < max) {
            binding.tvAdd.setVisibility(View.VISIBLE);
            binding.tvDel.setVisibility(View.VISIBLE);
        } else {
            binding.tvAdd.setVisibility(View.GONE);
            binding.tvDel.setVisibility(View.VISIBLE);
        }

        binding.itemTvUnitTime.setContent(priceSetEntity.getTimeName());
        binding.itemTvPriceDriver.setContent(priceSetEntity.getDriverPrice());
        binding.itemTvPriceDriver.setLabelRight(priceSetEntity.getPriceUnitName());
        binding.itemTvPriceDriver.setVisibility(isDriver ? View.VISIBLE : View.GONE);
        binding.itemTvPriceDriverNot.setContent(priceSetEntity.getNoDriverPrice());
        binding.itemTvPriceDriverNot.setLabelRight(priceSetEntity.getPriceUnitName());
        selectedMap.put(priceSetEntity.getTimeNo(), priceSetEntity.getTimeName());//补充未加入的初始化选择项
        //小数点后保留两位数字
        binding.itemTvPriceDriver.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvPriceDriverNot.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvPriceDriver.setFilters(new EditInputFilter[]{new EditInputFilter(999999999.99, 2)});
        binding.itemTvPriceDriverNot.setFilters(new EditInputFilter[]{new EditInputFilter(999999999.99, 2)});
        binding.itemTvPriceDriverNot.setFocusableInTouchMode(true);
        binding.itemTvPriceDriverNot.setFocusable(true);
        binding.itemTvUnitTime.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                if (timeUnitList == null || timeUnitList.size() == 0) return;
                OptionsPickerView businessOption = new OptionsPickerBuilder(context, (options1, options2, options3, v) -> {
                    if (isSelected(timeUnitList.get(options1))) {
                        MToast.showToast(context, "该单位已被使用,请重新选择");
                        return;
                    }
                    String content = timeUnitList.get(options1).getProjectscale();
                    List<PickSelectEntity> children = timeUnitList.get(options1).getChildren();
                    String contentChildren = children.get(0).getProjectscale();
                    //设置对应的价格单位
                    binding.itemTvPriceDriver.setLabelRight(contentChildren);
                    binding.itemTvPriceDriverNot.setLabelRight(contentChildren);
                    priceSetEntity.setPriceUnitNo(children.get(0).getNo());
                    priceSetEntity.setPriceUnitName(children.get(0).getProjectscale());
                    //设置时间周期单位
                    binding.itemTvUnitTime.setContent(content);
                    priceSetEntity.setTimeName(content);
                    priceSetEntity.setTimeNo(timeUnitList.get(options1).getNo());

                }).build();
                businessOption.setPicker(timeUnitList);
                businessOption.setSelectOptions(0);
                businessOption.show();
            }
        });

        binding.itemTvPriceDriver.setViewOnFocusChangedListener(new ItemInputTextView.OnFocusChangedListener() {
            @Override
            public void onFocusChanged(View view, boolean focused) {
                if (!focused) {
                    priceSetEntity.setDriverPrice(binding.itemTvPriceDriver.getContent());
                }
            }
        });

        binding.itemTvPriceDriverNot.setViewOnFocusChangedListener(new ItemInputTextView.OnFocusChangedListener() {
            @Override
            public void onFocusChanged(View view, boolean focused) {
                if (!focused) {
                    priceSetEntity.setNoDriverPrice(binding.itemTvPriceDriverNot.getContent());
                }
            }
        });

        binding.tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.size() < 4) {
                    datas.add(getCheckAdd());
                    notifyDataSetChanged();
                }
            }
        });

        binding.tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.size() > 1) {
                    selectedMap.remove(priceSetEntity.getTimeNo());
                    datas.remove(position);
                    notifyDataSetChanged();
                }
            }
        });

    }

    private PriceSetEntity getCheckAdd() {
        PriceSetEntity setEntity = new PriceSetEntity();
        for (PickSelectEntity<List<PickSelectEntity>> listPriceSetEntity : timeUnitList) {
            if (!selectedMap.containsKey(listPriceSetEntity.getNo())) {
                setEntity.setTimeNo(listPriceSetEntity.getNo());
                setEntity.setTimeName(listPriceSetEntity.getProjectscale());
                setEntity.setPriceUnitNo(listPriceSetEntity.getChildren().get(0).getNo());
                setEntity.setPriceUnitName(listPriceSetEntity.getChildren().get(0).getProjectscale());
                selectedMap.put(listPriceSetEntity.getNo(), listPriceSetEntity.getProjectscale());
                return setEntity;
            }
        }
        return setEntity;
    }

    //判断是否已被选择
    private boolean isSelected(PickSelectEntity<List<PickSelectEntity>> entity) {
        checkSelected();
        if (selectedMap.containsKey(entity.getNo())) {
            return true;
        } else {
            selectedMap.put(entity.getNo(), entity.getProjectscale());
        }
        return false;
    }

    //重置选择记录,和当前实际列表一致
    private void checkSelected() {
        selectedMap.clear();
        for (int i = 0; i < datas.size(); i++) {
            selectedMap.put(datas.get(i).getTimeNo(), datas.get(i).getTimeName());
        }
    }
}
